﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;

namespace MixingStation
{
    public class CGlobal
    {
        public static string strSys_name;
        public static int nSafelock_time;  //minitue
        public static string strCur_user;
        public static string strPasswd;
        public static string strCur_deptid;
        public static string strCur_deptname;
        public static int keepPassword;
        public static DateTime dtLoginTime;
        public static string strJsonUrl;
        public static string strSwapJsonData = "";//Get/Put不同业务模块的JsonData中转，每次用完清空。
        public static string cookie = null;
        public static DateTime logindate = DateTime.Today;  //用来填入单据的日期

        public static Boolean bDebug = true;   //debug状态标志，json上下传和debug msg到debug.log。

        public static bool IsNumeric(string value)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(value, @"^[+-]?/d*[.]?/d*$");
        }
        public static bool IsInt(string value)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(value, @"^[+-]?/d*$");
        }
        public static bool IsUnsign(string value)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(value, @"^/d*[.]?/d*$");
        }

        /// <summary>
        ///if(CGlobal.bDebug)
        ///JSON IO debug信息都输出到@"\Application\wrWMSLog.txt
        /// </summary>
        /// <param name="strModule"></param>
        /// <param name="strType"></param>
        /// <param name="strLogMsg"></param>
        /// <returns></returns>
        public static bool PutDebugLog(string strModule, string strType, string strLogMsg)
        {
            string strDebugMsg = "";
            if (!CGlobal.bDebug)
                return true;
            else
                try
                {
                    FileStream aFile = new FileStream(@"\Application\Log.txt", FileMode.Append, FileAccess.Write);
                    StreamWriter sw = new StreamWriter(aFile, System.Text.Encoding.GetEncoding("gb2312"));
                    strDebugMsg = DateTime.Now.ToString() + ":" + CGlobal.strCur_user + ";" + strModule + ";" + strType + ";" + strLogMsg;

                    sw.WriteLine(strDebugMsg);
                    sw.Close();
                    return true;
                }
                catch
                {
                    return false;
                }
        }

        public static bool DelDebugLog()
        {
            try
            {
                if (File.Exists(@"\Application\Log.txt"))
                {
                    File.Delete(@"\Application\Log.txt");
                    return true;
                }
                else
                    return false;
            }
            catch
            {
                return false;
            }
        }

        static public bool Connected_To_Network()
        {
            IPEndPoint localEndPoint = new IPEndPoint(Dns.Resolve(Dns.GetHostName()).AddressList[0], 0);
            try
            {
                if (localEndPoint.Address.ToString() == "127.0.0.1")
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }

        public static string varifyBarcode(string barcode, string header)
        {
            string ret = "";
            try
            {
                int i = barcode.Split((char)10).Length;
                if (i < 1 || barcode.Split((char)10)[0] != "WL")
                {
                    ret = "无效条码";
                    return ret;
                }
            }
            catch
            {
                ret = "条码解析错误";
                return ret;
            }
            return ret;
        }

        /// <summary>
        /// 
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        struct SYSTEMTIME
        {
            public ushort wYear;
            public ushort wMonth;
            public ushort wDayOfWeek;
            public ushort wDay;
            public ushort wHour;
            public ushort wMinute;
            public ushort wSecond;
            public ushort wMilliseconds;
        }

        [DllImport("coredll.dll")]
        private static extern bool SetLocalTime(ref SYSTEMTIME lpSystemTime);
        [DllImport("coredll.dll")]
        private static extern bool GetLocalTime(ref SYSTEMTIME lpSystemTime);

        public static void SetSysTime(DateTime date)
        {
            SYSTEMTIME lpTime = new SYSTEMTIME();
            lpTime.wYear = Convert.ToUInt16(date.Year);
            lpTime.wMonth = Convert.ToUInt16(date.Month);
            lpTime.wDay = Convert.ToUInt16(date.Day);
            lpTime.wHour = Convert.ToUInt16(date.Hour);
            lpTime.wMinute = Convert.ToUInt16(date.Minute);
            lpTime.wSecond = Convert.ToUInt16(date.Second);
            SetLocalTime(ref lpTime);
        }
    }
}
